<template>
  <PageWrapper>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-space :size="12">
          <a-button
            type="primary"
            @click="operateApplication(operateActionType.ADD)"
            v-auth="permissionCodeMap.applicationManageAdd"
          >
            新增应用
          </a-button>
        </a-space>
      </template>
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'requestUrl'">
          <EllipsisTooltip :title="record.requestUrl" :width="180">
            <a-typography-link :href="record.requestUrl" target="_blank"> {{ record.requestUrl }}</a-typography-link>
          </EllipsisTooltip>
        </template>
        <template v-else-if="column.dataIndex === 'appPlatform'">
          <span v-if="record.appPlatform === 0">否</span>
          <span v-else-if="record.appPlatform === 1">是</span>
        </template>
        <template v-else-if="column.dataIndex === 'appStatus'">
          <span class="global-active-status" v-if="record.appStatus === 0">正常</span>
          <span class="global-stop-status" v-else-if="record.appStatus === 1">禁用</span>
        </template>
        <template v-else-if="column.dataIndex === 'operate'">
          <a-button
            type="link"
            v-auth="permissionCodeMap.applicationManageEdit"
            class="g-operate-btn"
            @click="operateApplication(operateActionType.EDIT, record)"
          >
            编辑
          </a-button>
        </template>
      </template>
    </BasicTable>
    <ApplicationAdd @register="applicationAddRegister" @operateSuccess="operateSuccess" />
  </PageWrapper>
</template>

<script lang="ts" setup>
import { reactive, ref, computed, unref, onMounted } from 'vue'
import PageWrapper from '@/components/PageWrapper/index.vue'
import { BasicTable, useTable } from '@/components/Table'
import { getApplicationBasicColumns, getSearchFormConfig } from './dataSource'
import { useDrawer } from '@/components/Drawer'
import ApplicationAdd from './components/ApplicationAdd.vue'
import EllipsisTooltip from '@/components/EllipsisTooltip/index.vue'
import { permissionCodeMap } from '@/utils/permissionCodeMap'
import { operateActionType, operateTxtType, constantMap } from '@/types/enum'
import { ApplicationRecord } from '@/types/deployUpgrade'
import { message } from 'ant-design-vue'
import api from '@/api'
const { findAppList } = api

const [applicationAddRegister, { openDrawer: applicationAddOpenDrawer, setDrawerProps }] = useDrawer()

const [registerTable, { reload, setPagination, getForm }] = useTable({
  api: findAppList,
  columns: getApplicationBasicColumns(),
  useSearchForm: true,
  formConfig: getSearchFormConfig(),
  scroll: { y: constantMap.tableYHeight, x: constantMap.tableXWidth }
})

const operateApplication = (type: OperateType, record?: ApplicationRecord) => {
  setDrawerProps({
    title: operateTxtType[type] + '应用'
  })
  applicationAddOpenDrawer(true, {
    type,
    record
  })
}
// 操作成功回调
const operateSuccess = () => {
  setPagination({
    current: 1
  })
  reload()
}
</script>

<style lang="less" scoped></style>
